import util from "../../utils/util.js";
import {
  amendPasswdModel,
  resetPasswdModel
} from '../../models/password.js'
import {
  sendCodeModel
} from '../../models/common.js'
import {
  phoneValidate
} from '../../models/validate.js'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    lastPassword: '',
    passWord: '',
    passWordEnd: '',
    code: '',
    phone: '',
    repass: true,
    sendTime: true,
    send: '获取验证码'
  },
  //提交密码
  submitCode() {
    if (this.data.repass) {
      this.amendPasswd()
    } else {
      this.resetPasswd()
    }
  },
  //修改密码
  amendPasswd() {
    const lastPass = this.data.lastPassword
    const pass = this.data.passWord
    const passEnd = this.data.passWordEnd
    console.log(pass, passEnd)
    if (pass.length == 6 && lastPass.length == 6) {
      if (pass === passEnd) {
        //修改
        this._amendPasswdModel(lastPass, pass)
      } else {
        this._showModalContent('两次输入密码不一致')
      }
    } else {
      this._showModalContent('密码长度不足')
    }
  },
  // 修改密码回调函数
  _amendPasswdModel(lastPass, pass) {
    amendPasswdModel(lastPass, pass).then(res => {
      if (res.errorCode == 0) {
        return this._showModalSucc()
      } else {
        return this._showModalContent('原密码输入错误')
      }
    })
  },
  //发送验证码
  sendCode() {
    const phone = this.data.phone
    const template=util.odeTepmpale.reset
    const sendTime = this.data.sendTime
    const state = phoneValidate(phone)
    if (state) {
      if (sendTime) {
        this._sendTime(false)
        sendCodeModel(phone,template).then(res => {
          if (res.errorCode == 0) {
            this._codeTime();
          } else {
            util.showToast('短信发送失败');
            this._sendTime(true)
          }
        })
      }
    }
  },
  // 重置密码
  resetPasswd() {
    const code = this.data.code
    const phone = this.data.phone
    const pass = this.data.passWord
    const passEnd = this.data.passWordEnd
    if (code == '') {
      return this._showModalContent('验证码不能为空')
    }
    if (pass.length == 6) {
      if (pass === passEnd) {
        //重置
        this._resetPasswd(phone, code, pass)
      } else {
        this._showModalContent('两次输入密码不一致')
      }
    } else {
      this._showModalContent('密码长度不足')
    }
  },
  //重置密码回调函数
  _resetPasswd(phone, code, pass) {
    console.log(phone, code, pass)
    resetPasswdModel(phone, code, pass).then(res => {
      if (res.errorCode == 10000) {
        if (res.msg.captcha) {
          return util.showToast('验证码错误')
        }
        if (res.msg.phone) {
          return util.showToast('手机号与绑定手机号不一致')
        }
      }
      if (res.errorCode == 0){
        this._showModalSucc() 
      }
    })

  },
  //选择修改或重置密码
  operation() {
    const repass = !this.data.repass
    if (repass) {
      wx.setNavigationBarTitle({
        title: '修改密码'
      })
    } else {
      wx.setNavigationBarTitle({
        title: '重置密码'
      })
    }
    this.setData({
      repass
    })
  },
  //验证码倒计时
  _codeTime() {
    let num = 60;
    setInterval(() => {
      num--;
      if (num > 0) {
        this.setData({
          send: "" + num + "s"
        });
      }
      if (num == 0) {
        this._sendTime(true)
        this.setData({
          send: "获取验证码"
        });
      }
    }, 1000);
  },
  //修改成功
  _showModalSucc() {
    const state = this.data.repass == true ? '修改' : '重置'
    wx.showModal({
      title: '提醒',
      content: `您已${state}成功，自动为您跳转到登录页`,
      showCancel: false,
      confirmText: '我知道了',
      success() {
        wx.navigateTo({
          url: '/pages/index/index'
        })
      }
    })
  },
  //状态
  _showModalContent(content) {
    wx.showModal({
      content: content,
      showCancel: false
    })
  },
  // 更改发送时间状态
  _sendTime(state) {
    this.setData({
      sendTime: state
    })
  },
  //更新手机号码
  upPhone(e) {
    this.setData({
      phone: e.detail.value
    })
  },
  //更新验证码
  upCode(e) {
    this.setData({
      code: e.detail.value
    })
  },
  //原密码更新
  upLastPass(e) {
    this.setData({
      lastPassword: e.detail.value
    })
  },
  //更新密码
  upPass(e) {
    this.setData({
      passWord: e.detail.value
    })
  },
  //再次输入密码更新
  upPassEnd(e) {
    this.setData({
      passWordEnd: e.detail.value
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})